/**
 * Title: UserController.java
 *
 * @author wujin
 * @date 2019年12月30日
 * @version 1.0
 */
package com.pm.controller.security;

import com.pm.common.Response;
import com.pm.common.annotation.CurrentUser;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.IndexBean;
import com.pm.entity.po.security.User;
import com.pm.service.security.UserService;
import com.pm.utils.StringUtils;
import com.pm.utils.security.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Title: UserController
 *
 * @author WuJin
 * @date 2019年12月30日
 */
@Api(tags = "登录退出管理")
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 登录 Title: addUser
     *
     * @param user
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    @MyLog(value = "登录")
    @ApiOperation("登录")
    public Response<IndexBean> login(@RequestParam String username, @RequestParam String password) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return new Response<>(ExpCodeState.login_failed_name_isnull);
        }
        User user = new User(username, password);
//		MD5Util.encrypt(user);
        IndexBean bean = userService.login(user);
        Response<IndexBean> response = null;
        if (bean == null) {
            response = new Response<IndexBean>(ExpCodeState.login_failed);
        } else {
            response = new Response<IndexBean>(ExpCodeState.login_success, bean);
        }
        return response;
    }

    @UserLoginToken
    @RequestMapping("/logout")
    @ResponseBody
    @ApiOperation("退出登录")
    @MyLog(value = "退出登录")
    public Response<Integer> logout(@CurrentUser User user) {
        JwtTokenUtil.createJwtToken(user.getUsername());
        Response<Integer> response = new Response<Integer>(ExpCodeState.loginout, 1);
        return response;
    }
}
